﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using BLL;

namespace DAL.Managers
{
    /// <summary>
    /// The manager for the objet EbookFile
    /// </summary>
    public class EbookFileManager
    {
        #region properties

        private static EbookFileManager _sInstance;
        private static readonly object SInstanceLocker = new object();

        #endregion

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static EbookFileManager Instance
        {
            get
            {
                lock (SInstanceLocker)
                {
                    return _sInstance ?? (_sInstance = new EbookFileManager());
                }
            }
        }

        #region public functions

        /// <summary>
        /// Gets the files for an ebook.
        /// </summary>
        /// <param name="idBook">The id book.</param>
        /// <param name="path">The path of the folder of the book.</param>
        /// <param name="conn">The connection to the database.</param>
        /// <returns>The list of the downloadables files.</returns>
        public List<EbookFile> GetFilesByEbook(Int64 idBook, string path, SQLiteConnection conn = null)
        {
            List<EbookFile> list;

            if (conn != null)
            {
                // Treatment
                list = GetFilesByEbookTreatment(idBook, path, conn);
            }
            else
            {
                using (conn = new SQLiteConnection(Connection.CatalogConnection))
                {
                    conn.Open();

                    // Treatment
                    list = GetFilesByEbookTreatment(idBook, path, conn);
                }
            }

            return list;
        }

        #endregion

        #region private functions

        /// <summary>
        /// Gets the files for an ebook (Main Treatment).
        /// </summary>
        /// <param name="idBook">The id book.</param>
        /// <param name="path">The path of the folder of the book.</param>
        /// <param name="conn">The connection to the database (opened).</param>
        /// <returns>The list of the downloadables files.</returns>
        private static List<EbookFile> GetFilesByEbookTreatment(Int64 idBook, string path, SQLiteConnection conn)
        {
            var list = new List<EbookFile>();
            
            var query = "SELECT id, name, format FROM data WHERE book=" + idBook;
            var cmd = new SQLiteCommand(query, conn);

            // Construction of the files
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                // New file
                var file = new EbookFile();
                file.Id = reader.GetInt64(0);
                file.Path = path + "/" + reader.GetString(1);
                file.Format = reader.GetString(2);

                list.Add(file);
            }

            // Clear the memory
            reader.Dispose();
            cmd.Dispose();

            return list;
        }

        #endregion

        
    }
}
